<?php
namespace Home\Model;
use Think\Model;
/**
 * Created by PhpStorm.
 * User: 97628
 * Date: 2016/6/19
 * Time: 12:37
 */
class zhihuModel extends Model{
    /**
     * if: answerId != 0 得到该条数据
     * else: 得到随机一条数据
     * @param $answerId
     * @return mixed
     */
    function getRandom($answerId){
        do {
            if($answerId){
                $condition['answerId'] = $answerId;
            }else{
                $condition['Id'] = mt_rand(1, 1287);
            }
            $res = M('zhihu')->where($condition)->select();
        }while(sizeof($res) == 0 && $answerId);
        return $res[0];
    }

    /**
     * 得到简略信息
     * @param $answerId string
     * @return mixed
     */
    function getPreview($answerId){
        $condition['answerId'] = $answerId;
        $res = M('zhihu')->where($condition)->getField('Id, title, author, answerId');
        foreach($res as $v) {
            return $v;
        }
    }

}